package com.jxwifi.cloud.quickclean.utils;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;


/**
 * @author oushuhua
 */
public class KAlertDialog {
    private TextView mTvTitle;
    private TextView mTvContent;
    private TextView mTvLeftBtn;
    private TextView mTvRightBtn;
    private Dialog mDialog;

    public KAlertDialog(Context context) {
        this(context, "");
    }

    public KAlertDialog(Context context, String content) {
        if (mDialog == null) {
            mDialog = new Dialog(context, com.idroid.R.style.loading_dialog);
            View root = View.inflate(context, com.idroid.R.layout.dialog_custom, null);
            mTvTitle = (TextView) root.findViewById(com.idroid.R.id.tv_dialog_title);
            mTvContent = (TextView) root.findViewById(com.idroid.R.id.tv_dialog_content);
            mTvLeftBtn = (TextView) root.findViewById(com.idroid.R.id.tv_dialog_leftbtn);
            mTvRightBtn = (TextView) root.findViewById(com.idroid.R.id.tv_dialog_rightbtn);
            mDialog.setContentView(root);
        }
        if (!TextUtils.isEmpty(content)) mTvContent.setText(content);
        mTvLeftBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        mTvRightBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    public KAlertDialog setContentText(String txt) {
        mTvContent.setText(txt);
        return this;
    }

    public KAlertDialog setTitleText(String txt) {
        mTvTitle.setVisibility(View.VISIBLE);
        mTvTitle.setText(txt);
        return this;
    }

    public void setCancelable(boolean cancelable) {
        mDialog.setCancelable(cancelable);
    }

    /**
     * 设置左边btn的文本、点击事件。不需要设置就传null
     */
    public KAlertDialog setLeftTxt(String txt, View.OnClickListener listener) {
        if (null != txt) {
            mTvLeftBtn.setText(txt);
        }
        if (null != listener) {
            mTvLeftBtn.setOnClickListener(listener);
        }
        dismiss();
        return this;
    }

    public KAlertDialog leftTxtGone(){
        mTvLeftBtn.setVisibility(View.GONE);
        return this;
    }
    public KAlertDialog rightTxtGone(){
        mTvRightBtn.setVisibility(View.GONE);
        return this;
    }

    /**
     * 设置右边btn的文本、点击事件。不需要设置就传null
     */
    public KAlertDialog setRightTxt(String txt, View.OnClickListener listener) {
        if (null != txt) {
            mTvRightBtn.setText(txt);
        }
        if (null != listener) {
            mTvRightBtn.setOnClickListener(listener);
        }
        dismiss();
        return this;
    }

    public void show() {
        if (mDialog != null) {
            if(mTvTitle.getText().toString().isEmpty())
                mTvTitle.setVisibility(View.GONE);
            if(mTvContent.getText().toString().isEmpty())
                mTvContent.setVisibility(View.GONE);
            mDialog.show();
            mDialog.getWindow().setLayout(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
        }
    }

    public void dismiss() {
        if (mDialog != null)
            mDialog.dismiss();
    }

    public boolean isShowing() {
        return mDialog.isShowing();
    }

}
